package yicheng.dinnertogetherapp;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import yicheng.dinnertogetherapp.R;

public class PreferenceActivity extends Activity{
	EditText personal_preference_editText;
	LinearLayout personal_preference_layout;
	
	AmazonClientManager clientManager;
	
	SharedPreferences local_user_information;
	SharedPreferences.Editor local_user_editor;
	String PREFS_NAME = "LocalUserInfo";
	
	String PHONE_NUMBER, PREFERENCE;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		clientManager = new AmazonClientManager();
		setContentView(R.layout.personal_preference_view);
		overridePendingTransition(R.anim.activity_left_in,R.anim.activity_left_out);
		
		local_user_information =  this.getSharedPreferences(PREFS_NAME,0);
		personal_preference_editText = (EditText) findViewById(R.id.personal_preference_editText);
		
		updatePreferenceDisplay();
		
		
		
		
		
		personal_preference_layout = (LinearLayout) findViewById(R.id.personal_preference_layout);
		personal_preference_layout.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				hideKeyboard();
				return false;
			}
		});
		
		
		
	}
	
	
	
	private void updatePreference(){
		PHONE_NUMBER = local_user_information.getString("PhoneNumber", "default");
		runOnUiThread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new Thread(new Runnable(){

					@Override
					public void run() {
						// TODO Auto-generated method stub
						SimpleDB.updateSingleAttribute(clientManager, PHONE_NUMBER,SimpleDB.PREFERENCE_ITEM, SimpleDB.PREFERENCE_ATTRIBUTE, personal_preference_editText.getText().toString());
					
					}
					
				}).start();
			}
			
		});
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		updatePreference();
		
		local_user_information =  this.getSharedPreferences(PREFS_NAME,0);
		local_user_editor = local_user_information.edit();
		local_user_editor.putString("Preference", personal_preference_editText.getText().toString());
		local_user_editor.commit();
		
		finish();
	}
	
	
	private void hideKeyboard(){
		if(getCurrentFocus()!=null) {
			InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
		}
	}
	
	
	private void updatePreferenceDisplay(){
		PREFERENCE = local_user_information.getString("Preference", "My Preference");
		personal_preference_editText.setText(PREFERENCE);
		
	}
	
	@Override
	public void onBackPressed() {
	    super.onBackPressed();
	    overridePendingTransition(R.anim.activity_right_in, R.anim.activity_right_out);
	}
	
	

}
